package com.usernet.product.server;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;

import com.usernet.product.dao.SoftwareDao;
import com.usernet.product.entity.Download;
import com.usernet.product.entity.Software;
import com.usernet.product.tools.DownloadType;
import com.usernet.product.tools.JSONTools;
import com.usernet.product.utils.ProductConfig;

public class DownloadService {

	private SoftwareDao softwareDao = new SoftwareDao();

	public String download(HttpServletRequest request) {
		String ret = null;
		try {
			String softId = request.getParameter("softId");
			String userName = request.getParameter("userName");
			Software software = (Software) softwareDao.getById(Software.class,
					Integer.parseInt(softId));
			// 更新下载次数
			software.setCount(software.getCount() + 1);
			if (softwareDao.modify(software)) {
				// 增加下载记录
				Download download = new Download();
				String tel = MarketUtils.getIpAddr(request);
				if (userName == null) {
					userName = "手机游客";
				}
				download.setSoftName(software.getName());
				download.setUserName(userName);
				download.setTel(tel);
				download.setType(DownloadType.DOWN_OR_SEA_FROM_MOBILE);
				download.setTime(new Timestamp(System.currentTimeMillis()));
				download.setStatus(0);
				softwareDao.save(download);
			}
			String downPath = ProductConfig.downloadPath
					+ software.getDownload();
			ret = MarketUtils.getResJson("\"" + JSONTools.string2Json(downPath)
					+ "\"");
		} catch (Exception ex) {
			ex.printStackTrace();
			ret = MarketUtils.getThrowJson();
		}
		return ret;
	}
}
